/*
 * Feedback.cpp
 *
 *      Author: Christian
 *      Letztes Update: 03.11.13 Christian | Code Aufräumen
 *
 */

#include "Feedback.h"


//ton in Hz, dauer in 10 ms
static void summer_ton( uint16_t ton, uint8_t dauer ) {
	//Frequenz in Periodendauer umrechnen:
	uint16_t periode = 10000 / ton;

	//Anzahl Hauptschleifendurchl�ufe errechnen:
	uint16_t anzahl = 100 * dauer / periode;

	if ( SUMMER_STATUS ) {
		for ( uint16_t i = 0; i <= anzahl ; i++ ) {
			SUMMER_PORT |= ( 1 << SUMMER_PIN );

			for ( uint8_t j = 0 ; j < periode; j++ ) {
				_delay_us( 40 );
			}

			SUMMER_PORT &= ~( 1 << SUMMER_PIN );
			for ( uint8_t j = 0 ; j < periode; j++ ) {
				_delay_us( 60 );
			}
		}
	}
}



void feedback_init( void ) {
	LED_DDR |= ( 1 << LED_PIN );
	if ( SUMMER_STATUS ) {
		SUMMER_DDR |= ( 1 << SUMMER_PIN );
	}

	/* Alte schaebige Melodie
	status_led_on();
	summer_ton( 6 , 2 );
	status_led_off();
	status_led_on();
	summer_ton( 5 , 2 );
	summer_ton( 4 , 2 );
	summer_ton( 5 , 2 );
	*/

	//Neue Star-Wars-Melodie
	status_led_on();
	summer_ton( 1567, 10 );
	_delay_ms( 10 );

	status_led_off();
	summer_ton( 1864, 10 );
	_delay_ms( 10 );

	status_led_on();
	summer_ton( 2349, 10 );
	_delay_ms( 10 );

	status_led_off();
	summer_ton( 3135, 10 );
	_delay_ms( 10 );

	status_led_on();
	summer_ton( 2349, 10 );
	_delay_ms( 10 );

	status_led_off();
	summer_ton( 3135, 30 );
	_delay_ms( 10 );

}

void i2c_error( void ) {
	if ( SUMMER_STATUS ) {
		summer_ton( 2217 , 10 );

		_delay_ms( 50 );
		summer_ton( 3135, 10 );
		_delay_ms( 100 );
	}
}

void mpu_ready() {
	if ( SUMMER_STATUS ) {
	summer_ton( 1760 , 20 );
	_delay_ms(50);
	summer_ton( 2637 , 10 );
	}
}

void status_led_on( void ) {

		LED_PORT |= ( 1 << LED_PIN );

}

void status_led_off( void ) {

	LED_PORT &= ~( 1 << LED_PIN );

}


void system_pause_signal() {
	if ( SUMMER_STATUS ) {
		summer_ton( 2637 , 10 );

		_delay_ms( 50 );
		summer_ton( 1760 , 20 );
		_delay_ms( 50 );
	}
}
